package com.ly.hotel.view.liuwei;

import java.io.File;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.Constants;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.SysConf;
import com.ly.hotel.msgevent.CloseHandEvent;
import com.ly.hotel.msgevent.HandNoInputCompleteEvent;
import com.ly.hotel.view.floatWindows.ShuaKaFloatWin;

import de.greenrobot.event.EventBus;

public class InputHandNoActivity extends AlertDialog {

	private EditText txtArtNo;
	private TextView tipsTxt;
	private Button cancelBtn;
	private ImageView tipsImage;
	LinearLayout keyBoardContainer;
	private String digitnum = "";
	private Button digitkeypad_1;  
    private Button digitkeypad_2;  
    private Button digitkeypad_3;  
    private Button digitkeypad_4;  
    private Button digitkeypad_5;  
    private Button digitkeypad_6;  
    private Button digitkeypad_7;  
    private Button digitkeypad_8;  
    private Button digitkeypad_9;  
    private Button digitkeypad_0;  
    private Button digitkeypad_c;  
    private Button digitkeypad_ok;  
    
	MyHandler mHandler = new MyHandler();
	private Context context;
	private boolean threadFlag = true;
	
	private int length = 3;
	public InputHandNoActivity(Context context) {
		super(context);
		this.context = context;
	}

	public InputHandNoActivity(Context context, int theme) {
		super(context, theme);
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_input_handno);
		EventBus.getDefault().register(this);
		initView();
	}

	@Override
	protected void onStart() {
		super.onStart();

		new Thread() {
			public void run() {
				MyApplication.clearBandNo();
				while (MyApplication.getBandNo() == "" && threadFlag) {
					try {
						Thread.sleep(100); 
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				} 
				
				ShuaKaFloatWin.hideShuakaWin();
				Message message = new Message();
				message.what = 1;
				mHandler.sendMessage(message);
				Log.e("inputHand", "InputHandNoActivity->set Text to HandNo");
			}
		}.start();

	}

	private void initView() {
		txtArtNo = (EditText) findViewById(R.id.handNoTxt);
		tipsTxt  = (TextView) findViewById(R.id.tipsTxt);
		tipsImage = (ImageView) findViewById(R.id.liuwei_tips);
		keyBoardContainer = (LinearLayout) findViewById(R.id.liuwei_key_board);
		
		if(SysConf.extendConfGetValue(SysConf.SYS_CONF_SHOUPAI_ENABLE, "0").equals("1")){
			ShuaKaFloatWin.showShuakaWin();
			tipsTxt.setText(context.getString(R.string.SwipeTips));

			if (MyApplication.getSeatBitmap() != null) {
				tipsImage.setBackgroundDrawable(new BitmapDrawable(MyApplication.getSeatBitmap()));
			} else {
				File ff = new File(Constants.SEAT_FILE_PATH);
				if (ff.exists()) {
					Bitmap logoImage = BitmapFactory.decodeFile(Constants.SEAT_FILE_PATH);
					if (logoImage != null) {
						tipsImage.setBackgroundDrawable(new BitmapDrawable(logoImage));
						MyApplication.setSeatBitmap(logoImage);
						logoImage = null;
					}
				}else{
					tipsImage.setBackgroundResource(R.drawable.shuaka_tips);
				}
			}
		}else{
			tipsTxt.setText(context.getString(R.string.guaji_password_tips));
			txtArtNo.setInputType(InputType.TYPE_CLASS_NUMBER);
			keyBoardContainer.setVisibility(View.VISIBLE);
			tipsImage.setVisibility(View.GONE); 
			setup();
		}
		
		
		this.cancelBtn = (Button) findViewById(R.id.cancelBtn);
		cancelBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				InputHandNoActivity.this.dismiss();
			}
		});

		
	}

    public void setup() {  
        // 初始化 对象  
        digitkeypad_1 = (Button) findViewById(R.id.digitkeypad_1);  
        digitkeypad_2 = (Button) findViewById(R.id.digitkeypad_2);  
        digitkeypad_3 = (Button) findViewById(R.id.digitkeypad_3);  
        digitkeypad_4 = (Button) findViewById(R.id.digitkeypad_4);  
        digitkeypad_5 = (Button) findViewById(R.id.digitkeypad_5);  
        digitkeypad_6 = (Button) findViewById(R.id.digitkeypad_6);  
        digitkeypad_7 = (Button) findViewById(R.id.digitkeypad_7);  
        digitkeypad_8 = (Button) findViewById(R.id.digitkeypad_8);  
        digitkeypad_9 = (Button) findViewById(R.id.digitkeypad_9);  
        digitkeypad_0 = (Button) findViewById(R.id.digitkeypad_0);  
        digitkeypad_c = (Button) findViewById(R.id.digitkeypad_c);  
        digitkeypad_ok = (Button) findViewById(R.id.digitkeypad_ok);  
   
        // 添加点击事件  
        DigitPasswordKeypadOnClickListener dkol = new DigitPasswordKeypadOnClickListener();  
        digitkeypad_1.setOnClickListener(dkol);
        digitkeypad_2.setOnClickListener(dkol);  
        digitkeypad_3.setOnClickListener(dkol);  
        digitkeypad_4.setOnClickListener(dkol);  
        digitkeypad_5.setOnClickListener(dkol);  
        digitkeypad_6.setOnClickListener(dkol);  
        digitkeypad_7.setOnClickListener(dkol);  
        digitkeypad_8.setOnClickListener(dkol);  
        digitkeypad_9.setOnClickListener(dkol);  
        digitkeypad_0.setOnClickListener(dkol);  
        digitkeypad_c.setOnClickListener(dkol);  
        digitkeypad_ok.setOnClickListener(new DigitPasswordKeypadFinshOnClikcListener());  
        digitkeypad_1.requestFocus();
    }  
   
    private class DigitPasswordKeypadFinshOnClikcListener implements View.OnClickListener {  

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			MyApplication.setBandNo(txtArtNo.getText().toString());
			Message message = new Message();
			message.what = 1;
			mHandler.sendMessage(message);
		}  
    }  

    private class DigitPasswordKeypadOnClickListener implements View.OnClickListener {  

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int viewId = v.getId();  
            switch (viewId) {
            case R.id.digitkeypad_1:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 1;  
                }  
                break;  
            case R.id.digitkeypad_2:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 2;  
                }  
                break;  
            case R.id.digitkeypad_3:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 3;  
                }  
                break;  
            case R.id.digitkeypad_4:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 4;  
                }  
                break;  
            case R.id.digitkeypad_5:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 5;  
                }  
                break;  
            case R.id.digitkeypad_6:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 6;  
                }  
                break;  
            case R.id.digitkeypad_7:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 7;  
                }  
                break;  
            case R.id.digitkeypad_8:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 8;  
                }  
                break;  
            case R.id.digitkeypad_9:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 9;  
                }  
                break;  
            case R.id.digitkeypad_0:  
                if (digitnum.length() == length) {  
                    return;  
                } else {  
                    digitnum += 0;  
                }  
   
                break;  
            case R.id.digitkeypad_c:// 后退  
                if (digitnum.length() > 0) {  
                    digitnum = digitnum.substring(0, digitnum.length() - 1);  
                }  
                break;  
            }  
            // 格式化 数据  
            txtArtNo.setText(digitnum);  
            txtArtNo.setSelection(null != digitnum ? digitnum.length() : 0);  
		}  
   
    } 
    
	public void onEventMainThread(CloseHandEvent e) {
		this.dismiss();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		threadFlag = false; //退出没有介绍的线程
		EventBus.getDefault().unregister(this);
		super.onStop();
	}

	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				// 更新本页面文本框显示读取到的卡号
				if(MyApplication.getBandNo().equals("")){
					if(this != null)
						dismiss();
					
					return;
				}
				
				txtArtNo.setText(MyApplication.getBandNo());
				// 打开
				Log.e("inputHand", "发送消息弹出挂机页面.");
				EventBus.getDefault().post(new HandNoInputCompleteEvent(MyApplication.getBandNo()));
				
				GuaJiActivity dialog = new GuaJiActivity(context.getApplicationContext(), R.style.msgDialog);// 创建Dialog并设置样式主题
				dialog.setId(MyApplication.getBandNo());
				dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
				dialog.show();
				
				MyApplication.clearBandNo();
				try{
					if(this != null)
						dismiss();
				}catch (Exception e) {
					Log.e("inputHand", "隐藏刷卡界面失败.");
				}
				break;
			}
		}
	};
}
